/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inject;

import commonpackage.Eating;
import commonpackage.Hosting;
import commonpackage.Position;
import commonpackage.Travel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import util.SQLHelper;

/**
 *
 * @author chuda
 */
public class Injection {
    
    private static String DESCRIPTION = "Description";
    private static String NAME = "Name";
    private static String URL = "URL";
    private static String LATITUDE = "Latitude";
    private static String LONGITUDE = "Longitude";
    private static String TYPE = "Type";
    private static String CAFFE = "Cafe";
    private static String RESTAURANT = "Restaurant";
    private static String TELEPHONE = "Telephone";
    private static String ADDRESS = "Address";
    private static String CHARACTERISTICS = "Characteristics";
    private static String ACCOMODATION = "Accomodation";
    private static String MIN_PRICE = "Min_Price";
    private static String MAX_PRICE = "Max_Price";
    private static String AVG_PRICE = "Avg_Price";
    private static String HOTEL = "Hotel";
    private static String RESIDENCE = "Residencia";
    private static String MIN_PRICE_NIGHT = "Min_Price_Night";
    private static String MAX_PRICE_NIGHT = "Max_Price_Night";
    private static String NUM_AVAIL_ROOMS = "Num_Available_Rooms";
    private static String LUNCH = "Lunch_Included";
    private static String CAMPING = "Camping";
    private static String MONUMENT = "Monument";
    private static String PARK = "Park";
    private static String COUNTRY = "Country";
    private static String CITY = "City";
    private static String PRICE = "Price";

    public static void main(String[]args) {

        try {
          Class.forName("com.mysql.jdbc.Driver");
          String connectionUrl = "jdbc:mysql://localhost/mrc2009?" +
                                   "user=mrc2009&password=mrc2009";
          Connection con = DriverManager.getConnection(connectionUrl);
          Statement stmt = con.createStatement();
          SAXBuilder builder = new SAXBuilder();
          Document doc = builder.build("pois.xml");
          Element root = doc.getRootElement();
          List children = root.getChildren();
          for(int i=0;i<children.size();i++){
              Element el = (Element) children.get(i);
             // double id = Double.parseDouble(el.getChildText("POIId"));
              String description = el.getChildText(DESCRIPTION);
              String name = el.getChildText(NAME);
              String url = el.getChildText(URL);
              float latitude = Float.parseFloat(el.getChildText(LATITUDE));
              float longitude = Float.parseFloat(el.getChildText(LONGITUDE));
              String telephone = el.getChildText(TELEPHONE);
              String address = el.getChildText(ADDRESS);
              String type = el.getChildText(TYPE);
              int ticket = 0;
              if(type.compareTo(RESTAURANT)==0 || type.compareTo(CAFFE)==0){
                  if(type.compareTo(RESTAURANT)==0)
                      ticket=1;
                //  String classif = Helper.EATING;
                  String characteristics = el.getChildText(CHARACTERISTICS);
                  int accomodation = Integer.parseInt(el.getChildText(ACCOMODATION));
                  float min_price = Float.parseFloat(el.getChildText(MIN_PRICE));
                  float max_price = Float.parseFloat(el.getChildText(MAX_PRICE));
                  float avg_price = Float.parseFloat(el.getChildText(AVG_PRICE));
                  Eating eating = new Eating(type, name, description,address,telephone, true, new Position(longitude,latitude),
                          min_price, max_price, avg_price, accomodation);
                  eating.setUrl(url);
                  String sql = SQLHelper.insertPOI(eating);
                  stmt.execute(sql);
              }
              else if(type.compareTo("Hotel")==0 || type.compareTo("Residência")==0){
                  ticket=1;
                  float min_price_night = Float.parseFloat(el.getChildText("Min_Price_Night"));
                  float max_price_night = Float.parseFloat(el.getChildText("Max_Price_Night"));
                  int num_available_rooms = Integer.parseInt(el.getChildText("Num_Available_Rooms"));
                  int lunch_included = Integer.parseInt(el.getChildText("Lunch_Included"));
                  boolean lunch = false;
                  if(lunch_included>0)
                      lunch = true;
                  Hosting hosting = new Hosting(type,name, description,address,telephone,true, new Position(longitude,latitude),
                          min_price_night,max_price_night,num_available_rooms,lunch);
                  hosting.setUrl(url);
                  String sql = SQLHelper.insertPOI(hosting);
                  stmt.execute(sql);
                  //stmt.execute("INSERT INTO resting VALUES ('"+id+"','"+min_price_night+"','"+max_price_night+"','"+num_available_rooms+"','"+lunch_included+"','"+description+"','"+ticket+"','"+latitude+"','"+longitude+"','"+type+"')");
              }
              else if(type.compareTo("Camping")==0 || type.compareTo("Monument")==0 ||type.compareTo("Park")==0){
                  ticket=0;
                  String country = el.getChildText("Country");
                  String city = el.getChildText("City");
                  float price = Float.parseFloat(el.getChildText("Price"));
                  // public Travel (String type,String descr,boolean order,
                  //Position pos, String country, String city, double price)
                  Travel travel = new Travel(type, name,description,address,telephone,false,new Position(longitude,latitude),
                          country,city,price);
                  travel.setUrl(url);
                  String sql = SQLHelper.insertPOI(travel);
                  stmt.execute(sql);
                  //stmt.execute("INSERT INTO travel VALUES ('"+id+"','"+country+"','"+city+"','"+price+"','"+description+"','"+ticket+"','"+latitude+"','"+longitude+"','"+type+"')");

              }
             // System.out.println(id);
          }


        }catch (Exception e) {
            e.printStackTrace();
        }


    }

}
